﻿using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace IceDog.DNC.Web.Swashbuckle1_X.Convention
{
    /// <summary>
    /// 根据命名空间区分版本的控制器模型约定
    /// </summary>
    public class ApiExplorerGroupPerVersionConvention : IControllerModelConvention
    {
        /// <summary>
        /// 构建根据命名空间区分版本的约定的实例
        /// </summary>
        public ApiExplorerGroupPerVersionConvention()
        {
            
        }
        /// <summary>
        /// Called to apply the convention to the <see cref="T:Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerModel" />.
        /// </summary>
        /// <param name="controller">The <see cref="T:Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerModel" />.</param>
        public void Apply(ControllerModel controller)
        {
            var controllerNamespace = controller.ControllerType.Namespace; // e.g. "ControllersV1"
            var apiVersion = controllerNamespace.Split('V').Last().ToLower();
            controller.ApiExplorer.GroupName = "v"+apiVersion;
        }
    }
}
